﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JoyServer.Net
{
	/// <summary>
	/// クライアントサーバ間で送受信するパケット
	/// </summary>
	public class Packet
	{
		/// <summary>
		/// パケットタイプ
		/// </summary>
		public uint type;

		/// <summary>
		/// 未受信データ
		/// (フレームワーク内で使用)
		/// </summary>
		public uint remain;

		/// <summary>
		/// 関連付けるタグ
		/// (フレームワーク内では未使用)
		/// </summary>
		public String tag;

		/// <summary>
		/// 受信したデータ
		/// </summary>
		public ByteArray data;

		/// <summary>
		/// コンストラクタ
		/// </summary>
		public Packet()
		{
			type = 0;
			data = new ByteArray();
			data.position = 0;
		}

		/// <summary>
		/// パケットのコピー
		/// </summary>
		/// <returns></returns>
		public Packet clone()
		{
			Packet packet = new Packet();
			packet.type      = type;
			packet.remain    = remain;
			packet.tag       = tag;
			packet.data.writeBytes(data, 0, data.length);
			packet.data.position = 0;
		
			return packet;
		}

		/// <summary>
		/// 文字列形式で内容を表す
		/// </summary>
		/// <returns></returns>
		public String toString()
		{
			String text;
			text  = "Packet {";
			text += "type: " + type + ", ";
			text += "remain: " + remain + ", ";
			text += "tag: " + tag + ", ";
			text += "data.length: " + data.length + ", ";
			text += "data.position: " + data.position + " ";
			text += "}";
			
			return text;
		}
	}
}
